export function timeFix () {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function welcome () {
  const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了']
  const index = Math.floor(Math.random() * arr.length)
  return arr[index]
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent () {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader (callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () {}
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate (id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}

// 格式化时间 count为天数，返回count天后的时间，包括当前时间（t1为当前时间，t2为count天后的时间）
export function timeForMatAdd (count) {
  var time1 = new Date()
  time1.setTime(time1.getTime())
  var Y1 = time1.getFullYear()
  var M1 = ((time1.getMonth() + 1) >= 10 ? (time1.getMonth() + 1) : '0' + (time1.getMonth() + 1))
  var D1 = (time1.getDate() >= 10 ? time1.getDate() : '0' + time1.getDate())
  var H1 = (time1.getHours() >= 10 ? time1.getHours() : '0' + time1.getHours())
  var Min1 = (time1.getMinutes() >= 10 ? time1.getMinutes() : '0' + time1.getMinutes())
  var S1 = (time1.getSeconds() >= 10 ? time1.getSeconds() : '0' + time1.getSeconds())
  var timer1 = Y1 + '-' + M1 + '-' + D1 + '' + H1 + ':' + Min1 + ':' + S1// 当前时间
  var time2 = new Date()
  time2.setTime(time2.getTime() + (24 * 60 * 60 * 1000 * (count - 1)))
  var Y2 = time2.getFullYear()
  var M2 = ((time2.getMonth() + 1) >= 10 ? (time2.getMonth() + 1) : '0' + (time2.getMonth() + 1))
  var D2 = (time2.getDate() >= 10 ? time2.getDate() : '0' + time2.getDate())
  var H2 = (time2.getHours() >= 10 ? time2.getHours() : '0' + time2.getHours())
  var Min2 = (time2.getMinutes() >= 10 ? time2.getMinutes() : '0' + time2.getMinutes())
  var S2 = (time2.getSeconds() >= 10 ? time2.getSeconds() : '0' + time2.getSeconds())
  var timer2 = Y2 + '-' + M2 + '-' + D2 + '' + H2 + ':' + Min2 + ':' + S2 // 之前的7天或者30天
  return {
    t1: timer1,
    t2: timer2
  }
}
// 格式化时间 年-月-日 时：分：秒
export function datetimes6 (n) {
  const date = new Date(n)
  const nowDate = new Date()
  const nowY = nowDate.getFullYear()
  const Y = date.getFullYear()
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
  const D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  const h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  const m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  const s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  if (Y < nowY) {
    return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
  } else {
    return M + '-' + D + ' ' + h + ':' + m + ':' + s
  }
}

// 生成UUID
export function uuid () {
  var s = []
  var hexDigits = '0123456789abcdef'
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
  }
  s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = '-'

  var uuid = s.join('')
  return uuid
}
